package com.ww.hadoop.mr.topn;

import lombok.Data;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * 自定义类型必须实现接口： 序列化/反序列化   比较器
 */
@Data
public class TKey implements WritableComparable<TKey> {


    private int year;
    private int month;
    private int day;
    private int wd;


    @Override
    public int compareTo(TKey that) {
        //默认比较逻辑
        //the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y
        int c1 = Integer.compare(this.year, that.getYear());
        if(c1 == 0){
            int c2 = Integer.compare(this.month, that.getMonth());
            if(c2 == 0){
                return   Integer.compare(this.day,that.getDay());
            }
            return  c2;
        }
        return c1;

    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(year);
        out.writeInt(month);
        out.writeInt(day);
        out.writeInt(wd);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.year = in.readInt();
        this.month = in.readInt();
        this.day = in.readInt();
        this.wd = in.readInt();
    }
}
